# -*- coding: utf-8 -*-
# @Time    : 2021/9/16 13:26
# @Author  : BrandonGT
# @FileName: 10_11_favorite_number.py

import json

FILE_NAME = "user_favorite_number.json"


def store_favorite_number():
    """储存用户输入的数字，检查输入是否为 数字"""
    prompt = "Please enter your favorite number: "
    number = input(prompt)
    try:
        number = int(number)
    except ValueError:
        print("Please enter numerical input")
    else:
        number = str(number)
        with open(FILE_NAME, "w") as f:
            json.dump(number, f)


def retrieve_favorite_number():
    """获取用户储存的数字，并打印出结果"""
    try:
        with open(FILE_NAME) as f:
            contents = json.load(f)
    except FileNotFoundError:
        return None
        # print(f"File {FILE_NAME} doesn't exist")
    else:
        print(f"I know your favorite number! It's {contents}")
        return True


def favorite_number():
    """尝试获取本地用户的数字，不存在时提示用户保存数字"""
    if not retrieve_favorite_number():
        store_favorite_number()


favorite_number()